package com.sanpu.kxe.business.main.view;

import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.meiqia.meiqiasdk.util.MQUtils;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.contract.AboutContract;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.presenter.AboutPresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.utils.FileUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.library.imagechoose.utils.PermissionsUtils;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：在线咨询
 * Created by 薛勇军 on 2018/7/10.
 */

public class AboutActivity extends BaseTitleActivity implements AboutContract.View {

    @Bind(R.id.serviceWeChatImagePathIv)
    ImageView serviceWeChatImagePathIv;
    @Bind(R.id.serviceWeChatNumberTv)
    TextView serviceWeChatNumberTv;

    private AboutContract.Presenter mPresenter;

    private String serviceNumber = MainConfig.SERVICE_NUMBER;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_about;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("在线咨询");

        mPresenter = new AboutPresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        UserConfigOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (output != null) {
            if (!TextUtils.isEmpty(output.getServiceWeChatNumber())) {
                serviceNumber = output.getServiceWeChatNumber();
            }
            if (!TextUtils.isEmpty(output.getServiceWeChatImagePath())) {
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + output.getServiceWeChatImagePath(), serviceWeChatImagePathIv);
            }
        }
        if (!TextUtils.isEmpty(serviceNumber)) {
            serviceWeChatNumberTv.setText("客服微信：" + serviceNumber + "");
        }
    }

    @OnClick({R.id.copyRtv, R.id.saveRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.copyRtv:
                if (android.os.Build.VERSION.SDK_INT > 11) {
                    android.content.ClipboardManager c = (android.content.ClipboardManager) mContext
                            .getSystemService(CLIPBOARD_SERVICE);
                    c.setPrimaryClip(ClipData.newPlainText("txt", serviceNumber));
                } else {
                    android.text.ClipboardManager c = (android.text.ClipboardManager) mContext
                            .getSystemService(CLIPBOARD_SERVICE);
                    c.setText(serviceNumber);
                }
                ToastUtil.showTextViewPrompt(mContext, "客服微信号复制成功");
                break;
            case R.id.saveRtv:
                serviceWeChatImagePathIv.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = serviceWeChatImagePathIv.getDrawingCache();
                if (mClipScreen == null) return;
                if (!PermissionsUtils.checkWriteStoragePermission(mContext))
                    return;
                File result = FileUtil.saveWechatToGallery(mContext, mClipScreen);
                ToastUtil.showTextViewPrompt(result != null ? "图片保存成功!请在相册查看" : "图片保存失败");
                break;
            default:
                break;
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CommonConfig.WRITE_EXTERNAL_STORAGE_REQUEST_CODE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mPresenter.conversationWrapper();
                } else {
                    MQUtils.show(AboutActivity.this, com.meiqia.meiqiasdk.R.string.mq_sdcard_no_permission);
                }
                break;
            }
        }

    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AboutContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
